const net = require('net')

const scoket = new net.Socket({})

scoket.connect({
  host: '127.0.0.1',
  port: 4000,
})

let lessonid = [136797, 136798, 136799, 136800, 136801, 136802, 136803, 136804, 136805]

let id = Math.floor(Math.random() * lessonid.length)

scoket.on('data', buffer => {
  // console.log(buffer.toString())
  const seqBuffer = buffer.slice(0, 2)
  const titleBuffer = buffer.slice(2)
  id = Math.floor(Math.random() * lessonid.length)

  console.log(seqBuffer.readInt16BE(), titleBuffer.toString())
  scoket.write(encode(id))
})

let seq = 0
function encode(index) {
  buffer = Buffer.alloc(6)
  buffer.writeInt16BE(seq++)
  buffer.writeInt32BE(lessonid[index], 2)
  return buffer
}

scoket.write(encode(id))
